package com.power.using.mydaggerp.presenter.movie;

import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.power.using.mydaggerp.cache.Cache;
import com.power.using.mydaggerp.contract.movie.MovieMainContract;
import com.power.using.mydaggerp.model.bean.douban.movie.HotMovieBean;
import com.power.using.mydaggerp.model.bean.douban.movie.child.SubjectsBean;
import com.power.using.mydaggerp.model.movie.MovieMainModel;
import com.power.using.mydaggerp.ui.activity.detial.MovieDetailActivity;
import com.power.using.mydaggerp.ui.fragment.movie.child.top.TopMoiveFragment;

import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2019/10/16.
 * Time: 2019/10/16  15:19
 */
public class MovieMainPresenter extends MovieMainContract.MovieMainPresenter{

    @NonNull
    public static MovieMainPresenter newInstance() {
        return new MovieMainPresenter();
    }


    @Override
    public void loadHotMovieList() {

        if(mIModel==null||mIView==null){
            return;
        }

        mRxManage.register(mIModel.getHotMovieList().subscribe(new Consumer<HotMovieBean>() {
            @Override
            public void accept(HotMovieBean hotMovieBean) throws Exception {
                if(mIView==null){
                    return;
                }

                mIView.updateContentList(hotMovieBean.getSubjects());
                Cache.saveHotMovieCache(hotMovieBean.getSubjects());


            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView!=null){

                    if(mIView.isVisiable()){
                        mIView.showToast("Network error.");
                    }

                    if(Cache.getHotMovieCache().size()==0){
                        mIView.showNetworkError();//没有缓存缓存，显示网络错误界面
                    }else{
                        mIView.updateContentList(Cache.getHotMovieCache());//加载缓存
                    }



                }
            }
        }));

    }

    @Override
    public void onItemClick(int position, SubjectsBean item, ImageView imageView) {

        MovieDetailActivity.start(mIView.getBindActivity(), item, imageView);
    }

    @Override
    public void onHeaderClick() {
        mIView.startNewFragment(TopMoiveFragment.newInstance());

    }

    @Override
    protected MovieMainContract.IMovieMainModel getModel() {
        return MovieMainModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
